package cn.wolfcode.wms.controller;

import cn.wolfcode.wms.domain.Product;
import cn.wolfcode.wms.qo.ProductQueryObject;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.service.IProductService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.util.UploadUtil;
import cn.wolfcode.wms.vo.JSONResult;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletContext;
import java.util.Map;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private ServletContext ctx;

    @RequestMapping("/list")
    @RequiredPermission("商品列表")
    public String list(@ModelAttribute("qo") ProductQueryObject qo, Map map) {
        map.put("brands", brandService.listAll());
        map.put("pageResult", productService.query(qo));
        return "product/list";
    }
    @RequestMapping("/selectProductList")
    @RequiredPermission("选择商品列表")
    public String selectProductList(@ModelAttribute("qo") ProductQueryObject qo, Map map) {
        map.put("brands", brandService.listAll());
        map.put("pageResult", productService.query(qo));
        return "product/selectProductList";
    }

    @RequestMapping("/input")
    @RequiredPermission("商品编辑")
    public String input(Long id, Map map) {
        map.put("brands", brandService.listAll());
        if (id != null) {
            map.put("product", productService.getById(id));
        }
        return "product/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission("更新或新增商品")
    public String saveOrUpdate(Product product, MultipartFile img) {
        try {
            if (img !=null && img.getSize()>0 &&!StringUtils.isEmpty(product.getImagePath())){
                UploadUtil.deleteFile(ctx,product.getImagePath());
            }
            if (img != null) {
                String imgPath = UploadUtil.upload(img, ctx.getRealPath("/upload"));
                product.setImagePath(imgPath);
            }
            productService.saveOrUpdate(product);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/product/list.do";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiredPermission("删除商品")
    public JSONResult delete(Long id, String imagePath) {
        try {
            if (!StringUtils.isEmpty(imagePath)){
                UploadUtil.deleteFile(ctx,imagePath);
            }
            productService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            // 出错时返回错误信息
            return JSONResult.mark(e.getMessage());
        }
        return JSONResult.success("删除成功！");
    }
}
